<?php
require_once ($DOCUMENT_ROOT . '/includes/functions.php');

session_start ();

// deal with the facebook users
if ($auth_token)
{
	require_once ($DOCUMENT_ROOT . '/includes/fb_api.php');
	$fb = new FacebookRestClient ('3217f2d4097bfb23d20fa82ec74a1310', '62afe3f66c8956c23cd5660785ab9823');
	// get the user id and session crap
	$sesh = $fb->auth_getSession($auth_token);
	
if ($sesh)
{
	$user = $sesh['uid'];
	$pass = "rat_pack44+";
	
	// see if fb id exists, if not, create new account on bobcat
	if (!login ($user, $pass))
		register($user, $pass);
		
	// set the session variable to fb_user
	$_SESSION['fb_user'] = true;
}
else
{
do_header("error", "");
echo "<p>your facebook session has expired<br />
please <a href=\"http://api.facebook.com/login.php?api_key=3217f2d4097bfb23d20fa82ec74a1310\" title=\"relogin with facebook\">relogin with facebook</a></p>";
do_footer();
exit;
}
}
//deal with regular users
else
{
	$user = $_POST['user'];
	$pass = $_POST['pass'];
}

if (login ($user, $pass))
{
	$_SESSION['valid_user'] = $user;

	do_header ('Login', '');
?>
	<img alt='bobcat logo' src='/images/small_logo.png'>
	<p>logged in 
<?php if ($_SESSION['fb_user'] == true) echo "from facebook"; else echo "as ".$_SESSION['valid_user']; ?></p>
	<p><a href="/list.php" title="continue to list">continue</a></p>
<?php
	do_footer ();
}
else
{
	do_header ("error", '');
	echo "<p>Failure: Error logging in</p>";
	do_url ('/', 'Try again');
	do_footer ();
	exit;
}

?>
